<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>

<template src="./index.html"></template>
<script>
    export default {
        components: {
            AppListBox: async () => Vue.extend(await Vue.prototype.$vFN.Components.Base.AppListBox()),
        },
        data() {
            return {
                appWindow:window,
                microApp: null,
                qkData: null,
                type: "",
            };
        },

        methods: {
            getType() {
                let _type = "";
                let _pathMatch = this.$route.params.pathMatch;
                if (_pathMatch) {
                    _type = _pathMatch.split("/")[0]
                }
                return _type;
            },
            
            async getPage() {
                let _type = this.getType();
                
                this.$vFN.Tools.getAppUseHistory();
                // 如果不处于此插件时重载新插件
                if (this.type != _type) {
                    if (this.microApp) {
                       await this.unmount()
                        this.microApp = null;
                        this.qkData = null;
                    }

                    this.type = _type;
                    if (_type) {
                        this.$vFN.Tools.setAppUseHistory(_type);
                        this.qkData = this.$vFN.Qk.appsConfig('apps', _type);
                        this.setConfig(0);
                    }
                }
                

            },
            setConfig(num_) {
                if (this.qkData) {

                    let _is = document.querySelector(this.qkData.app.container);
                    if (_is) {
                        this.microApp = this.$vFN.Qk.loadMicroApp(this.qkData.app, this.qkData.config)
                    } else if (num_ > 100) {
                        this.$vFN.Tools.setLoad(false);
                        this.$vFN.Tools.messageError("配置错误" + this.qkData.app.name + this.qkData.app.container + this
                            .qkData.app.entry)
                        return
                    } else {
                        setTimeout(() => {
                            this.setConfig(num_ + 1);
                        }, 1)
                    }
                }
            },
            async unmount() {
                if (this.microApp) {
                    await this.microApp.unmount();
                }
            },
           
        },

        computed: {},
       async mounted() {
            await this.$vFN.Tools.getAppAuthList()
            this.getPage();
        },
        activated() {},
        //    销毁后
        async destroyed() {
           await this.unmount()

        },
        watch: {
            $route: {
                handler(value) {
                    this.getPage();
                },
                deep: true,
            },
        },
    };
</script>